$(document).ready(function () {
    validateRule();

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }

    function login() {
        $.modal.loading($("#btnSubmit").data("loading"));
        var params = $("#signupForm").serializeObject();
        // console.log('form params', params);
        // return false
        $.ajax({
            type: "post",
            url: ctx + "login",
            data: params,
            success: function (r) {
                if (r.code == 0) {
                    location.href = ctx + 'index';
                } else {
                    $.modal.closeLoading();
                    $('.imgcode').click();
                    $(".code").val("");
                    $.modal.msg(r.msg);
                }
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            debug: true,

            rules: {
                name: {
                    required: true
                },
                code: {
                    required: true
                },
                address: {
                    required: true
                },
                connect: {
                    required: true
                },
                phone: {
                    required: true
                },
                workTime: {
                    required: true
                },
                workType: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: icon + "请输入您的企业名称"
                },
                code: {
                    required: icon + "请输入您的组织机构代码"
                },
                address: {
                    required: icon + "请输入您的详细地址"
                },
                connect: {
                    required: icon + "请输入您的姓名"
                },
                phone: {
                    required: icon + "请输入您的电话号码"
                },
                workTime: {
                    required: icon + "请输入您的申请返回时间"
                },
                workType: {
                    required: icon + "请输入您的返岗类别"
                }
            },
            submitHandler: function (form) {

                // console.log('form', form);
                // console.log('form params', form.serialize());
                login();
                return false
            },
        })
    }


});